library(tidyverse)
library(ggforce)
library(stringr)
library(cjoint)
library(cregg) # Install with remotes::install_github("leeper/cregg")
library(fixest)
library(lfe)
library(broom)
library(cowplot)
library(modelsummary)
library(scales)

# Set the directory to the location of the replication data
setwd("")

source("ggplot_theme.R")

# Was saved as rds rather than csv to keep factor level orderings
V <- read_rds("Data/Vignette_Data.rds")


########## VIGNETTE EXPERIMENT

# Many regressions are separated by citizen respondents and politicians
# respondents. This function runs the same regression model on both subsets
# and outputs results in a stacked data.frame for both
single_vignette <- function(outcome, data1, data2, label1, label2, group = "None",
                            formula = ~ text_pol + text_user + party_pol_copartisan + woman_pol + gendered + woman_user | country | 0 | id) {

    the_formula <- as.formula(paste0(outcome, " ~ ", as.character(formula)[2]))

    amce_model <- list()
    for(i in 1:2) {

        if(i == 1) data <- data1
        if(i == 2) data <- data2
        
        amce_model[[i]] <- felm(the_formula, data = data)

    }

    E1 <- get_estimates_vignette(amce_model[[1]])
    E2 <- get_estimates_vignette(amce_model[[2]])
    e1_n <- nobs(amce_model[[1]])
    e1_n_clusters <- length(unique(amce_model[[1]]$clustervar$id))

    e2_n <- nobs(amce_model[[2]])
    e2_n_clusters <- length(unique(amce_model[[2]]$clustervar$id))

    E <- bind_rows(tibble(E1, respondent_type = label1, group = group, n = e1_n, n_clusters = e1_n_clusters),
                   tibble(E2, respondent_type = label2, group = group, n = e2_n, n_clusters = e2_n_clusters))

    E <- E %>%
         mutate(feature = recode(feature, "gendered" = "Gendered text",
                                          "text_user" = "Reply from user",
                                          "woman_pol" = "Gender of politician",
                                          "woman_user" = "Gender of user",
                                          "party_pol_copartisan" = "Co-partisan politician",
                                          "gendered:woman_pol" = "Interaction",
                                          "woman_pol:woman_user" = "Interaction",
                                          "woman_pol:gendered" = "Interaction",
                                          "Interactions" = "Interaction"),
                level = recode(level, "Gendered text:Woman politician" = "Woman politician\nx gendered text",
                                      "Woman politician:Man user" = "Woman politician\nx male user",
                                      "Woman pol. x Gendered text" = "Woman politician\nx gendered text",
                                      "Woman pol. x Man user" = "Woman politician\nx male user")) %>%
          mutate(feature = factor(feature, levels = c("Interaction",
                                                      "Gender of politician",
                                                      "Gendered text",
                                                      "Gender of user",
                                                      "Co-partisan politician",
                                                      "Politician's text",
                                                      "Reply from user")))
    return(E)

}


# Function to run process regression models for graphing
# Deals with interactions; keeps baseline categories as a point at 0;
# renames variable names to plain English; etc.
get_estimates_vignette <- function(model) {

     out <- tidy(model, conf.int = TRUE) %>%
            rename("lower" = "conf.low", "upper" = "conf.high")

     out$feature <- NA
     out$feature[str_detect(out$term, "text_pol")] <- "Topic of politician's text"
     out$feature[str_detect(out$term, "gendered")] <- "Gendered text"
     out$feature[str_detect(out$term, "party_pol_copartisan")] <- "Co-partisan politician"
     out$feature[str_detect(out$term, "text_user")] <- "Reply from user"
     out$feature[str_detect(out$term, "woman_pol")] <- "Gender of politician"
     out$feature[str_detect(out$term, "woman_user")] <- "Gender of user"
     out$feature[str_detect(out$term, ":")] <- "Interactions"

     out$level <- out$term
     out$level <- str_replace_all(out$level, "text_pol_group|text_pol|party_pol_copartisan|gendered|text_user|woman_pol|woman_user", "")
     out$level <- str_replace_all(out$level, ":", " x ")
     out$level <- str_replace_all(out$level, "^Woman politician x", "Woman pol. x")

     current_levels <- out$level

     out <- out %>%
            add_row(estimate = 0, feature = "Topic of politician's text", level = "Economy") %>%
            add_row(estimate = 0, feature = "Co-partisan politician", level = "Non co-partisan") %>%
            add_row(estimate = 0, feature = "Gendered text", level = "Non-gendered text") %>%
            add_row(estimate = 0, feature = "Reply from user", level = "UserText01") %>%
            add_row(estimate = 0, feature = "Gender of politician", level = "Man politician") %>%
            add_row(estimate = 0, feature = "Gender of user", level = "Woman user")

     out$level <- factor(out$level,
                         levels = c(out$level[!out$level %in% current_levels], current_levels))

     out$feature <- factor(out$feature, levels = c("Interactions",
                                                   "Gender of politician",
                                                   "Gendered text",
                                                   "Gender of user",
                                                   "Co-partisan politician",
                                                   "Topic of politician's text",
                                                   "Reply from user"))

     return(out)

}




# FIGURE 6
model_prejudice <- single_vignette("prejudice",
                                   data1 = filter(V, respondent_type == "Politician"),
                                   data2 = filter(V, respondent_type == "Citizen"),
                                   label1 = "Politician sample", label2 = "Citizen sample",
                                   group = "Motivated by prejudice")
model_discourage_pol <- single_vignette("discourage_pol",
                                        data1 = filter(V, respondent_type == "Politician"),
                                        data2 = filter(V, respondent_type == "Citizen"),
                                        label1 = "Politician sample", label2 = "Citizen sample",
                                        group = "To discourage politician from being in politics")
model_opinion_diff <- single_vignette("opinion_diff",
                                      data1 = filter(V, respondent_type == "Politician"),
                                      data2 = filter(V, respondent_type == "Citizen"),
                                      label1 = "Politician sample", label2 = "Citizen sample",
                                      group = "Opinion difference with politician")
model_dislike_party <- single_vignette("dislike_party",
                                       data1 = filter(V, respondent_type == "Politician"),
                                       data2 = filter(V, respondent_type == "Citizen"),
                                       label1 = "Politician sample", label2 = "Citizen sample",
                                       group = "Dislike of the politician's party")
model_dissatisfied <- single_vignette("dissatisfied",
                                      data1 = filter(V, respondent_type == "Politician"),
                                      data2 = filter(V, respondent_type == "Citizen"),
                                      label1 = "Politician sample", label2 = "Citizen sample",
                                      group = "Dissatisfaction with own life")
model_troll_pol <- single_vignette("troll_pol",
                                   data1 = filter(V, respondent_type == "Politician"),
                                   data2 = filter(V, respondent_type == "Citizen"),
                                   label1 = "Politician sample", label2 = "Citizen sample",
                                   group = "To get a reaction from the politician")
model_troll_user <- single_vignette("troll_user",
                                    data1 = filter(V, respondent_type == "Politician"),
                                    data2 = filter(V, respondent_type == "Citizen"),
                                    label1 = "Politician sample", label2 = "Citizen sample",
                                    group = "To get a reaction from other users")

G1 <- bind_rows(filter(model_prejudice, feature == "Gender of politician" & !is.na(std.error)),
                filter(model_discourage_pol, feature == "Gender of politician" & !is.na(std.error)),
                filter(model_opinion_diff, feature == "Gender of politician" & !is.na(std.error)),
                filter(model_dislike_party, feature == "Gender of politician" & !is.na(std.error)),
                filter(model_dissatisfied, feature == "Gender of politician" & !is.na(std.error)),
                filter(model_troll_pol, feature == "Gender of politician" & !is.na(std.error)),
                filter(model_troll_user, feature == "Gender of politician" & !is.na(std.error)))

G1$group <- factor(G1$group, levels = rev(c("Motivated by prejudice",
                                            "To discourage politician from being in politics",
                                            "Opinion difference with politician",
                                            "Dislike of the politician's party",
                                            "Dissatisfaction with own life",
                                            "To get a reaction from the politician",
                                            "To get a reaction from other users")))
G1$respondent_type <- factor(G1$respondent_type, levels = c("Politician sample", "Citizen sample"))

# Figure 6 note
cat(paste0("\n\nAverage citizen sample observations~$=$~",
           scales::comma(mean(unique(G1$n[G1$respondent_type == "Citizen sample"]))),
           " (",
           scales::comma(mean(unique(G1$n_clusters[G1$respondent_type == "Citizen sample"]))),
           " respondents) per outcome; average politician sample observations~$=$~",
           scales::comma(mean(unique(G1$n[G1$respondent_type == "Politician sample"]))),
           " (",
           scales::comma(mean(unique(G1$n_clusters[G1$respondent_type == "Politician sample"]))),
           " respondents) per outcome.\n\n"))

# p-values for paragraph in article starting with "Results for the effect of a
# politician’s gender on each outcome are presented in Figure 6."
# Motivated by prejudice: p < 0.001 both
# To discourage politician from being in politics: p < 0.01 pol; p < 0.05 cit
# Opinion difference with politician: p < 0.05 pol; p = 0.25 cit
G1 %>% select("group", "respondent_type", "estimate", "p.value")


G1_graph <- ggplot(G1, aes(x = estimate, y = group, yend = group, color = respondent_type)) +
     my.theme(base_size = 8, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     facet_wrap(~ respondent_type) +
     labs(x = "Coefficient", y = "") +
     coord_cartesian(xlim = c(-0.15, 0.35)) +
     scale_x_continuous(breaks = seq(-0.1, 0.3, by = 0.1),
                        labels = c("-.1", "0", ".1", ".2", ".3")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(data = filter(G1, respondent_type == "Citizen sample"),
                size = 3, position = position_nudge(x = 0, y = 0)) +
     geom_point(data = filter(G1, respondent_type == "Politician sample"),
                size = 3, position = position_nudge(x = 0, y = 0)) +
     geom_segment(data = filter(G1, respondent_type == "Citizen sample"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0)) +
     geom_segment(data = filter(G1, respondent_type == "Politician sample"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0)) +
     scale_colour_manual(values = c("#5200FF", "#FF3E00", "#FFC3E8", "#3CFFCF", "#D6F41E")) +
     theme(legend.position = "none",
           strip.text.x = element_text(color = "transparent", angle = 0, hjust = 0.5, size = 8))

# FIGURE 6
pdf("Figures/Figure_6-RAW.pdf", 7, 2.45)
ggdraw() +
  draw_plot(G1_graph, x = 0, y = 0, width = 1, height = 1.02) +
  draw_plot_label(label = c("A.", "B."),
                x = c(0.35, 0.67), y = c(0.992, 0.992),
                size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Politician sample",
                            "Citizen sample"),
                  x = c(0.37, 0.69), y = c(0.992, 0.992),
                  fontface = "plain",
                  size = 8, hjust = 0, vjust = 1)
dev.off()


get_regression_table <- function(df) {

    ms_object <- data.frame(term = as.character(df$level[!is.na(df$std.error)]),
                                      estimate = df$estimate[!is.na(df$std.error)],
                                      std.error = df$std.error[!is.na(df$std.error)],
                                      p.value = df$p.value[!is.na(df$std.error)])
    ms_object$term <- str_replace_all(ms_object$term, "PolText", "Politician text ")
    ms_object$term <- str_replace_all(ms_object$term, "UserText", "User text ")
    ms_object$term <- str_replace_all(ms_object$term, " 0", " ")
    
    gl <- data.frame(`N observations` = scales::comma(unique(df$n)),
                     `N respondents` = scales::comma(unique(df$n_clusters)))

    ms_object <- list(tidy = ms_object,
                      glance = gl)
    class(ms_object) <- "modelsummary_list"

    return(ms_object)

}

# Complete regression results for Figure 6
ms_object_figure_6A_Pol <- get_regression_table(filter(model_prejudice, respondent_type == "Politician sample"))
ms_object_figure_6A_Cit <- get_regression_table(filter(model_prejudice, respondent_type == "Citizen sample"))

ms_object_figure_6B_Pol <- get_regression_table(filter(model_discourage_pol, respondent_type == "Politician sample"))
ms_object_figure_6B_Cit <- get_regression_table(filter(model_discourage_pol, respondent_type == "Citizen sample"))

ms_object_figure_6C_Pol <- get_regression_table(filter(model_opinion_diff, respondent_type == "Politician sample"))
ms_object_figure_6C_Cit <- get_regression_table(filter(model_opinion_diff, respondent_type == "Citizen sample"))

ms_object_figure_6D_Pol <- get_regression_table(filter(model_dislike_party, respondent_type == "Politician sample"))
ms_object_figure_6D_Cit <- get_regression_table(filter(model_dislike_party, respondent_type == "Citizen sample"))

ms_object_figure_6E_Pol <- get_regression_table(filter(model_dissatisfied, respondent_type == "Politician sample"))
ms_object_figure_6E_Cit <- get_regression_table(filter(model_dissatisfied, respondent_type == "Citizen sample"))

ms_object_figure_6F_Pol <- get_regression_table(filter(model_troll_pol, respondent_type == "Politician sample"))
ms_object_figure_6F_Cit <- get_regression_table(filter(model_troll_pol, respondent_type == "Citizen sample"))

ms_object_figure_6G_Pol <- get_regression_table(filter(model_troll_user, respondent_type == "Politician sample"))
ms_object_figure_6G_Cit <- get_regression_table(filter(model_troll_user, respondent_type == "Citizen sample"))



# TABLE N19
# Politician sample
modelsummary(list("Prejudice" = ms_object_figure_6A_Pol,
                  "Push politician out" = ms_object_figure_6B_Pol,
                  "Opinion differences" = ms_object_figure_6C_Pol,
                  "Dislike party" = ms_object_figure_6D_Pol,
                  "Dissatisfied w/ own life" = ms_object_figure_6E_Pol,
                  "Get reaction from politician" = ms_object_figure_6F_Pol,
                  "Get reaction from other users" = ms_object_figure_6G_Pol),
             coef_map = c("Woman politician", "Gendered text", "Man user", "Co-partisan",
                          paste0("Politician text ", 1:20),
                          paste0("User text ", 1:16)),
             stars = c("*" = .05, "**" = .01, "***" = .001),
             output = "latex")

# TABLE N20
# Citizen sample
modelsummary(list("Prejudice" = ms_object_figure_6A_Cit,
                  "Push politician out" = ms_object_figure_6B_Cit,
                  "Opinion differences" = ms_object_figure_6C_Cit,
                  "Dislike party" = ms_object_figure_6D_Cit,
                  "Dissatisfied w/ own life" = ms_object_figure_6E_Cit,
                  "Get reaction from politician" = ms_object_figure_6F_Cit,
                  "Get reaction from other users" = ms_object_figure_6G_Cit),
             coef_map = c("Woman politician", "Gendered text", "Man user", "Co-partisan",
                          paste0("Politician text ", 1:20),
                          paste0("User text ", 1:16)),
             stars = c("*" = .05, "**" = .01, "***" = .001),
             output = "latex")



# Separate out mechanism effects by respondent gender (among politicians)
model_prejudice_by_pol_resp_gender <- tibble(single_vignette("prejudice",
                                   data1 = filter(V, respondent_type == "Politician" & resp_gender == "Female"),
                                   data2 = filter(V, respondent_type == "Politician" & resp_gender == "Male"),
                                   label1 = "Female politician respondents", label2 = "Male politician respondents",
                                   group = "Motivated by prejudice"),
                                   respondent_sample = "Politicians")
model_prejudice_by_cit_resp_gender <- tibble(single_vignette("prejudice",
                                   data1 = filter(V, respondent_type == "Citizen" & resp_gender == "Female"),
                                   data2 = filter(V, respondent_type == "Citizen" & resp_gender == "Male"),
                                   label1 = "Female citizen respondents", label2 = "Male citizen respondents",
                                   group = "Motivated by prejudice"),
                                   respondent_sample = "Citizens")

model_discourage_pol_by_pol_resp_gender <- tibble(single_vignette("discourage_pol",
                                        data1 = filter(V, respondent_type == "Politician" & resp_gender == "Female"),
                                        data2 = filter(V, respondent_type == "Politician" & resp_gender == "Male"),
                                        label1 = "Female politician respondents", label2 = "Male politician respondents",
                                        group = "To discourage politician from being in politics"),
                                        respondent_sample = "Politicians")
model_discourage_pol_by_cit_resp_gender <- tibble(single_vignette("discourage_pol",
                                        data1 = filter(V, respondent_type == "Citizen" & resp_gender == "Female"),
                                        data2 = filter(V, respondent_type == "Citizen" & resp_gender == "Male"),
                                        label1 = "Female citizen respondents", label2 = "Male citizen respondents",
                                        group = "To discourage politician from being in politics"),
                                        respondent_sample = "Citizens")

model_opinion_diff_by_pol_resp_gender <- tibble(single_vignette("opinion_diff",
                                      data1 = filter(V, respondent_type == "Politician" & resp_gender == "Female"),
                                      data2 = filter(V, respondent_type == "Politician" & resp_gender == "Male"),
                                      label1 = "Female politician respondents", label2 = "Male politician respondents",
                                      group = "Opinion difference with politician"),
                                      respondent_sample = "Politicians")
model_opinion_diff_by_cit_resp_gender <- tibble(single_vignette("opinion_diff",
                                      data1 = filter(V, respondent_type == "Citizen" & resp_gender == "Female"),
                                      data2 = filter(V, respondent_type == "Citizen" & resp_gender == "Male"),
                                      label1 = "Female citizen respondents", label2 = "Male citizen respondents",
                                      group = "Opinion difference with politician"),
                                      respondent_sample = "Citizens")

model_dislike_party_by_pol_resp_gender <- tibble(single_vignette("dislike_party",
                                       data1 = filter(V, respondent_type == "Politician" & resp_gender == "Female"),
                                       data2 = filter(V, respondent_type == "Politician" & resp_gender == "Male"),
                                       label1 = "Female politician respondents", label2 = "Male politician respondents",
                                       group = "Dislike of the politician's party"),
                                       respondent_sample = "Politicians")
model_dislike_party_by_cit_resp_gender <- tibble(single_vignette("dislike_party",
                                       data1 = filter(V, respondent_type == "Citizen" & resp_gender == "Female"),
                                       data2 = filter(V, respondent_type == "Citizen" & resp_gender == "Male"),
                                       label1 = "Female citizen respondents", label2 = "Male citizen respondents",
                                       group = "Dislike of the politician's party"),
                                       respondent_sample = "Citizens")

model_dissatisfied_by_pol_resp_gender <- tibble(single_vignette("dissatisfied",
                                      data1 = filter(V, respondent_type == "Politician" & resp_gender == "Female"),
                                      data2 = filter(V, respondent_type == "Politician" & resp_gender == "Male"),
                                      label1 = "Female politician respondents", label2 = "Male politician respondents",
                                      group = "Dissatisfaction with own life"),
                                      respondent_sample = "Politicians")
model_dissatisfied_by_cit_resp_gender <- tibble(single_vignette("dissatisfied",
                                      data1 = filter(V, respondent_type == "Citizen" & resp_gender == "Female"),
                                      data2 = filter(V, respondent_type == "Citizen" & resp_gender == "Male"),
                                      label1 = "Female citizen respondents", label2 = "Male citizen respondents",
                                      group = "Dissatisfaction with own life"),
                                      respondent_sample = "Citizens")

model_troll_pol_by_pol_resp_gender <- tibble(single_vignette("troll_pol",
                                   data1 = filter(V, respondent_type == "Politician" & resp_gender == "Female"),
                                   data2 = filter(V, respondent_type == "Politician" & resp_gender == "Male"),
                                   label1 = "Female politician respondents", label2 = "Male politician respondents",
                                   group = "To get a reaction from the politician"),
                                   respondent_sample = "Politicians")
model_troll_pol_by_cit_resp_gender <- tibble(single_vignette("troll_pol",
                                   data1 = filter(V, respondent_type == "Citizen" & resp_gender == "Female"),
                                   data2 = filter(V, respondent_type == "Citizen" & resp_gender == "Male"),
                                   label1 = "Female citizen respondents", label2 = "Male citizen respondents",
                                   group = "To get a reaction from the politician"),
                                   respondent_sample = "Citizens")

model_troll_user_by_pol_resp_gender <- tibble(single_vignette("troll_user",
                                    data1 = filter(V, respondent_type == "Politician" & resp_gender == "Female"),
                                    data2 = filter(V, respondent_type == "Politician" & resp_gender == "Male"),
                                    label1 = "Female politician respondents", label2 = "Male politician respondents",
                                    group = "To get a reaction from other users"),
                                    respondent_sample = "Politicians")
model_troll_user_by_cit_resp_gender <- tibble(single_vignette("troll_user",
                                    data1 = filter(V, respondent_type == "Citizen" & resp_gender == "Female"),
                                    data2 = filter(V, respondent_type == "Citizen" & resp_gender == "Male"),
                                    label1 = "Female citizen respondents", label2 = "Male citizen respondents",
                                    group = "To get a reaction from other users"),
                                    respondent_sample = "Citizens")

G1_by_resp_gender <- bind_rows(filter(model_prejudice_by_pol_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               filter(model_prejudice_by_cit_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               
                               filter(model_discourage_pol_by_pol_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               filter(model_discourage_pol_by_cit_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               
                               filter(model_opinion_diff_by_pol_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               filter(model_opinion_diff_by_cit_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               
                               filter(model_dislike_party_by_pol_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               filter(model_dislike_party_by_cit_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               
                               filter(model_dissatisfied_by_pol_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               filter(model_dissatisfied_by_cit_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               
                               filter(model_troll_pol_by_pol_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               filter(model_troll_pol_by_cit_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               
                               filter(model_troll_user_by_pol_resp_gender, feature == "Gender of politician" & !is.na(std.error)),
                               filter(model_troll_user_by_cit_resp_gender, feature == "Gender of politician" & !is.na(std.error)))

G1_by_resp_gender$group <- factor(G1_by_resp_gender$group, levels = rev(c("Motivated by prejudice",
                                                                          "To discourage politician from being in politics",
                                                                          "Opinion difference with politician",
                                                                          "Dislike of the politician's party",
                                                                          "Dissatisfaction with own life",
                                                                          "To get a reaction from the politician",
                                                                          "To get a reaction from other users")))
G1_by_resp_gender$respondent_type <- factor(G1_by_resp_gender$respondent_type,
                                            levels = c("Female politician respondents",
                                                       "Male politician respondents",
                                                       "Female citizen respondents",
                                                       "Male citizen respondents"))

G1_by_resp_gender$respondent_sample <- factor(G1_by_resp_gender$respondent_sample,
                                            levels = c("Politicians",
                                                       "Citizens"))

G1_by_resp_gender %>% select("group", "respondent_type", "estimate", "p.value")

# Test interaction between respondent gender and woman politician
# In article paragraph beginning with "In Appendix H, we examine these
# results specifically for women and men politicians"
prejudice_pol_resp_gender_interaction <- felm(prejudice ~ text_pol + text_user + party_pol_copartisan + woman_pol * resp_gender + gendered + woman_user | country | 0 | id, data = filter(V, respondent_type == "Politician"))
summary(prejudice_pol_resp_gender_interaction) # p = 0.06

prejudice_cit_resp_gender_interaction <- felm(prejudice ~ text_pol + text_user + party_pol_copartisan + woman_pol * resp_gender + gendered + woman_user | country | 0 | id, data = filter(V, respondent_type == "Citizen"))
summary(prejudice_cit_resp_gender_interaction) # p = 0.62


# FIGURE H8
pdf("Figures/Mechanisms_by_repondent_gender_politicians-RAW.pdf", 6, 3.75)
ggplot(G1_by_resp_gender, aes(x = estimate, y = group, yend = group, color = respondent_type)) +
     my.theme(base_size = 8, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "Coefficient", y = "") +
     coord_cartesian(xlim = c(-0.25, 0.5)) +
     scale_x_continuous(breaks = seq(-0.2, 0.5, by = 0.1),
                        labels = c("-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(data = filter(G1_by_resp_gender, respondent_type == "Female politician respondents"),
                size = 3, position = position_nudge(x = 0, y = 0.15)) +
     geom_point(data = filter(G1_by_resp_gender, respondent_type == "Male politician respondents"),
                size = 3, position = position_nudge(x = 0, y = -0.15)) +
     geom_segment(data = filter(G1_by_resp_gender, respondent_type == "Female politician respondents"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0.15)) +
     geom_segment(data = filter(G1_by_resp_gender, respondent_type == "Male politician respondents"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = -0.15)) +
     geom_label(data = filter(G1_by_resp_gender, respondent_sample == "Politicians" & group == "Motivated by prejudice"),
                aes(x = lower, y = group, label = respondent_type),
          hjust = 1.05, vjust = 0.3, fontface = "bold",
          label.padding = unit(0.1, "lines"), label.size = NA,
               label.r = unit(0.15, "lines"),
          position = position_dodge(width = -0.8), size = 2.5, alpha = 0.7) +
     scale_colour_manual(values = c("Female politician respondents" = "#00BD8E",
                                    "Male politician respondents" = "#5200FF")) +
     theme(legend.position = "none",
           strip.text.x = element_text(color = "transparent", angle = 0, hjust = 0.5, size = 8))
dev.off()

# FIGURE H9
pdf("Figures/Mechanisms_by_repondent_gender_citizens-RAW.pdf", 6, 3.75)
ggplot(G1_by_resp_gender, aes(x = estimate, y = group, yend = group, color = respondent_type)) +
     my.theme(base_size = 8, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "Coefficient", y = "") +
     coord_cartesian(xlim = c(-0.25, 0.3)) +
     scale_x_continuous(breaks = seq(-0.2, 0.5, by = 0.1),
                        labels = c("-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(data = filter(G1_by_resp_gender, respondent_type == "Female citizen respondents"),
                size = 3, position = position_nudge(x = 0, y = 0.15)) +
     geom_point(data = filter(G1_by_resp_gender, respondent_type == "Male citizen respondents"),
                size = 3, position = position_nudge(x = 0, y = -0.15)) +
     geom_segment(data = filter(G1_by_resp_gender, respondent_type == "Female citizen respondents"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0.15)) +
     geom_segment(data = filter(G1_by_resp_gender, respondent_type == "Male citizen respondents"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = -0.15)) +
     geom_label(data = filter(G1_by_resp_gender, respondent_sample == "Citizens" & group == "Motivated by prejudice"),
                aes(x = lower, y = group, label = respondent_type),
          hjust = 1.05, vjust = 0.3, fontface = "bold",
          label.padding = unit(0.1, "lines"), label.size = NA,
               label.r = unit(0.15, "lines"),
          position = position_dodge(width = -0.8), size = 2.5, alpha = 0.7) +
     scale_colour_manual(values = c("Female citizen respondents" = "#00BD8E",
                                    "Male citizen respondents" = "#5200FF")) +
     theme(legend.position = "none",
           strip.text.x = element_text(color = "transparent", angle = 0, hjust = 0.5, size = 8))
dev.off()


# FIGURE 6 BY COUNTRY (FOR APPENDIX)
model_prejudice_country <- bind_rows(tibble(single_vignette("prejudice",
                                             data1 = filter(V, country == "Belgium" & respondent_type == "Politician"),
                                             data2 = filter(V, country == "Belgium" & respondent_type == "Citizen"),
                                             label1 = "Politician sample", label2 = "Citizen sample",
                                             group = "Motivated by prejudice"), country = "Belgium"),
                             tibble(single_vignette("prejudice",
                                                                data1 = filter(V, country == "Chile" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Chile" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Motivated by prejudice"), country = "Chile"),
                             tibble(single_vignette("prejudice",
                                                                data1 = filter(V, country == "Denmark" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Denmark" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Motivated by prejudice"), country = "Denmark"),
                             tibble(single_vignette("prejudice",
                                                                data1 = filter(V, country == "United States" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "United States" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Motivated by prejudice"), country = "US"))

model_discourage_pol_country <- bind_rows(tibble(single_vignette("discourage_pol",
                                             data1 = filter(V, country == "Belgium" & respondent_type == "Politician"),
                                             data2 = filter(V, country == "Belgium" & respondent_type == "Citizen"),
                                             label1 = "Politician sample", label2 = "Citizen sample",
                                             group = "To discourage politician from being in politics"), country = "Belgium"),
                             tibble(single_vignette("discourage_pol",
                                                                data1 = filter(V, country == "Chile" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Chile" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "To discourage politician from being in politics"), country = "Chile"),
                             tibble(single_vignette("discourage_pol",
                                                                data1 = filter(V, country == "Denmark" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Denmark" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "To discourage politician from being in politics"), country = "Denmark"),
                             tibble(single_vignette("discourage_pol",
                                                                data1 = filter(V, country == "United States" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "United States" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "To discourage politician from being in politics"), country = "US"))

model_opinion_diff_country <- bind_rows(tibble(single_vignette("opinion_diff",
                                             data1 = filter(V, country == "Belgium" & respondent_type == "Politician"),
                                             data2 = filter(V, country == "Belgium" & respondent_type == "Citizen"),
                                             label1 = "Politician sample", label2 = "Citizen sample",
                                             group = "Opinion difference with politician"), country = "Belgium"),
                             tibble(single_vignette("opinion_diff",
                                                                data1 = filter(V, country == "Chile" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Chile" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Opinion difference with politician"), country = "Chile"),
                             tibble(single_vignette("opinion_diff",
                                                                data1 = filter(V, country == "Denmark" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Denmark" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Opinion difference with politician"), country = "Denmark"),
                             tibble(single_vignette("opinion_diff",
                                                                data1 = filter(V, country == "United States" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "United States" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Opinion difference with politician"), country = "US"))
model_dislike_party_country <- bind_rows(tibble(single_vignette("dislike_party",
                                             data1 = filter(V, country == "Belgium" & respondent_type == "Politician"),
                                             data2 = filter(V, country == "Belgium" & respondent_type == "Citizen"),
                                             label1 = "Politician sample", label2 = "Citizen sample",
                                             group = "Dislike of the politician's party"), country = "Belgium"),
                             tibble(single_vignette("dislike_party",
                                                                data1 = filter(V, country == "Chile" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Chile" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Dislike of the politician's party"), country = "Chile"),
                             tibble(single_vignette("dislike_party",
                                                                data1 = filter(V, country == "Denmark" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Denmark" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Dislike of the politician's party"), country = "Denmark"),
                             tibble(single_vignette("dislike_party",
                                                                data1 = filter(V, country == "United States" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "United States" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Dislike of the politician's party"), country = "US"))
model_dissatisfied_country <- bind_rows(tibble(single_vignette("dissatisfied",
                                             data1 = filter(V, country == "Belgium" & respondent_type == "Politician"),
                                             data2 = filter(V, country == "Belgium" & respondent_type == "Citizen"),
                                             label1 = "Politician sample", label2 = "Citizen sample",
                                             group = "Dissatisfaction with own life"), country = "Belgium"),
                             tibble(single_vignette("dissatisfied",
                                                                data1 = filter(V, country == "Chile" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Chile" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Dissatisfaction with own life"), country = "Chile"),
                             tibble(single_vignette("dissatisfied",
                                                                data1 = filter(V, country == "Denmark" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Denmark" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Dissatisfaction with own life"), country = "Denmark"),
                             tibble(single_vignette("dissatisfied",
                                                                data1 = filter(V, country == "United States" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "United States" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "Dissatisfaction with own life"), country = "US"))
model_troll_pol_country <- bind_rows(tibble(single_vignette("troll_pol",
                                             data1 = filter(V, country == "Belgium" & respondent_type == "Politician"),
                                             data2 = filter(V, country == "Belgium" & respondent_type == "Citizen"),
                                             label1 = "Politician sample", label2 = "Citizen sample",
                                             group = "To get a reaction from the politician"), country = "Belgium"),
                             tibble(single_vignette("troll_pol",
                                                                data1 = filter(V, country == "Chile" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Chile" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "To get a reaction from the politician"), country = "Chile"),
                             tibble(single_vignette("troll_pol",
                                                                data1 = filter(V, country == "Denmark" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Denmark" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "To get a reaction from the politician"), country = "Denmark"),
                             tibble(single_vignette("troll_pol",
                                                                data1 = filter(V, country == "United States" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "United States" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "To get a reaction from the politician"), country = "US"))
model_troll_user_country <- bind_rows(tibble(single_vignette("troll_user",
                                             data1 = filter(V, country == "Belgium" & respondent_type == "Politician"),
                                             data2 = filter(V, country == "Belgium" & respondent_type == "Citizen"),
                                             label1 = "Politician sample", label2 = "Citizen sample",
                                             group = "To get a reaction from other users"), country = "Belgium"),
                             tibble(single_vignette("troll_user",
                                                                data1 = filter(V, country == "Chile" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Chile" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "To get a reaction from other users"), country = "Chile"),
                             tibble(single_vignette("troll_user",
                                                                data1 = filter(V, country == "Denmark" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "Denmark" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "To get a reaction from other users"), country = "Denmark"),
                             tibble(single_vignette("troll_user",
                                                                data1 = filter(V, country == "United States" & respondent_type == "Politician"),
                                                                data2 = filter(V, country == "United States" & respondent_type == "Citizen"),
                                                                label1 = "Politician sample", label2 = "Citizen sample",
                                                                group = "To get a reaction from other users"), country = "US"))

G1_country <- bind_rows(model_prejudice_country, model_discourage_pol_country,
                        model_opinion_diff_country, model_dislike_party_country,
                        model_dissatisfied_country, model_troll_pol_country,
                        model_troll_user_country)
G1_country <- bind_rows(G1_country, tibble(G1, country = "Pooled estimate"))

G1_country <- filter(G1_country, feature == "Gender of politician" & !is.na(std.error))

G1_country$country <- factor(G1_country$country, levels = c("Belgium", "Chile", "Pooled estimate", "Denmark", "US"))

G1_country$group <- factor(G1_country$group,
                           levels = c("Motivated by prejudice",
                                      "To discourage politician from being in politics",
                                      "Opinion difference with politician",
                                      "Dislike of the politician's party",
                                      "Dissatisfaction with own life",
                                      "To get a reaction from the politician",
                                      "To get a reaction from other users"))
G1_country$respondent_type <- factor(G1_country$respondent_type, levels = c("Politician sample", "Citizen sample"))


G1_country_graph_politician <- ggplot(filter(G1_country, respondent_type == "Politician sample"),
                           aes(x = estimate, y = group, yend = group,
                               fill = country, color = country,
                               shape = country, size = country)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     ggforce::facet_col( ~ group, scales = "free_y", space = "free") +
     labs(x = "Coefficient", y = "") +
     coord_cartesian(xlim = c(-0.25, 0.4)) +
     scale_y_discrete(breaks = c()) +
     scale_x_continuous(breaks = seq(-0.2, 0.4, by = 0.1),
                        labels = c("-.2", "-.1", "0", ".1", ".2", ".3", ".4")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_linerange(data = filter(G1_country, respondent_type == "Politician sample"),
                    aes(xmin = lower, xmax = upper),
                    position = position_dodge2(width = 1),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(G1_country, respondent_type == "Politician sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     scale_size_manual(values = c(0.75, 0.75, 2.5, 0.75, 0.75)) +
     scale_shape_manual(values = c(21, 21, 23, 21, 21)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "white", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "black", "#A1B811", "#5200FF")) +
     theme(strip.text.x = element_text(color = "black", angle = 0, hjust = 0, size = 7, face = "plain")) +
     guides(size = guide_legend(override.aes = list(size = 2)))

G1_country_graph_citizen <- ggplot(filter(G1_country, respondent_type == "Citizen sample"),
                           aes(x = estimate, y = group, yend = group,
                               fill = country, color = country,
                               shape = country, size = country)) +
     my.theme(base_size = 7, borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     ggforce::facet_col( ~ group, scales = "free_y", space = "free") +
     labs(x = "Coefficient", y = "") +
     coord_cartesian(xlim = c(-0.25, 0.4)) +
     scale_y_discrete(breaks = c()) +
     scale_x_continuous(breaks = seq(-0.2, 0.4, by = 0.1),
                        labels = c("-.2", "-.1", "0", ".1", ".2", ".3", ".4")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_linerange(data = filter(G1_country, respondent_type == "Citizen sample"),
                    aes(xmin = lower, xmax = upper),
                    position = position_dodge2(width = 1),
                    size = 0.5, show.legend = FALSE) +
     geom_point(data = filter(G1_country, respondent_type == "Citizen sample"),
                position = position_dodge2(width = 1), stroke = 0.8) +
     scale_size_manual(values = c(0.75, 0.75, 2.5, 0.75, 0.75)) +
     scale_shape_manual(values = c(21, 21, 23, 21, 21)) +
     scale_fill_manual(values = c("#00bd8e", "#FF3E00", "white", "#A1B811", "#5200FF")) +
     scale_colour_manual(values = c("#00bd8e", "#FF3E00", "black", "#A1B811", "#5200FF")) +
     theme(strip.text.x = element_text(color = "black", angle = 0, hjust = 0, size = 7, face = "plain")) +
     guides(size = guide_legend(override.aes = list(size = 2)))

# FIGURE E6
# Note: Edited in Illustrator to reposition legend
pdf("Figures/Main_Mechanisms_by_Country-RAW.pdf", 6, 6.5)
ggdraw() +
  draw_plot(G1_country_graph_politician, x = -0.01, y = 0.01, width = 0.5, height = 1) +
  draw_plot(G1_country_graph_citizen, x = 0.48, y = 0.01, width = 0.5, height = 1) +
  draw_plot_label(label = c("A. Politician sample",
                            "B. Citizen sample"),
                  x = c(0.01, 0.51), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1)
dev.off()





# MECHANISMS INTERACTIONS

model_prejudice_interactions_1 <- bind_rows(tibble(single_vignette("prejudice",
                                                                   data1 = filter(V, respondent_type == "Politician"),
                                                                   data2 = filter(V, respondent_type == "Citizen"),
                                                                   label1 = "Politician sample", label2 = "Citizen sample",
                                                                   group = "Motivated by prejudice",
                                                                   formula = ~ text_pol + text_user +
                                                                               party_pol_copartisan +
                                                                               woman_pol * gendered +
                                                                               woman_user + woman_pol |
                                                                               country | 0 | id)))
G3_prejudice_1 <- filter(model_prejudice_interactions_1,
                         feature %in% c("Interaction", "Gender of politician",
                                        "Gendered text"))

model_prejudice_interactions_2 <- bind_rows(tibble(single_vignette("prejudice",
                                                                   data1 = filter(V, respondent_type == "Politician"),
                                                                   data2 = filter(V, respondent_type == "Citizen"),
                                                                   label1 = "Politician sample", label2 = "Citizen sample",
                                                                   group = "Motivated by prejudice",
                                                                   formula = ~ text_pol + text_user +
                                                                               party_pol_copartisan +
                                                                               woman_pol * woman_user +
                                                                               woman_user + woman_pol |
                                                                               country | 0 | id)))
G3_prejudice_2 <- filter(model_prejudice_interactions_2,
                         feature %in% c("Interaction", "Gender of politician",
                                        "Gender of user"))


model_discourage_pol_interactions_1 <- bind_rows(tibble(single_vignette("discourage_pol",
                                                                   data1 = filter(V, respondent_type == "Politician"),
                                                                   data2 = filter(V, respondent_type == "Citizen"),
                                                                   label1 = "Politician sample", label2 = "Citizen sample",
                                                                   group = "Discourage politicians from being in politics",
                                                                   formula = ~ text_pol + text_user +
                                                                               party_pol_copartisan +
                                                                               woman_pol * gendered +
                                                                               woman_user + woman_pol |
                                                                               country | 0 | id)))
G3_discourage_pol_1 <- filter(model_discourage_pol_interactions_1,
                         feature %in% c("Interaction", "Gender of politician",
                                        "Gendered text"))

model_discourage_pol_interactions_2 <- bind_rows(tibble(single_vignette("discourage_pol",
                                                                   data1 = filter(V, respondent_type == "Politician"),
                                                                   data2 = filter(V, respondent_type == "Citizen"),
                                                                   label1 = "Politician sample", label2 = "Citizen sample",
                                                                   group = "Discourage politicians from being in politics",
                                                                   formula = ~ text_pol + text_user +
                                                                               party_pol_copartisan +
                                                                               woman_pol * woman_user +
                                                                               woman_user + woman_pol |
                                                                               country | 0 | id)))
G3_discourage_pol_2 <- filter(model_discourage_pol_interactions_2,
                         feature %in% c("Interaction", "Gender of politician",
                                        "Gender of user"))


# P-values of interactions for prejudice outcome
# Regarding results in paragraph beginning with "Finally, we investigate these
# results further by examining whether the extent to which a message sent to a
# woman politician is seen as driven by prejudice depends on"
# p < 0.001 for both interactions and samples
G3_prejudice_1[G3_prejudice_1$level == "Woman politician\nx gendered text", ] %>%
  select("respondent_type", "level", "estimate", "p.value")
G3_prejudice_2[G3_prejudice_2$level == "Woman politician\nx male user", ] %>%
  select("respondent_type", "level", "estimate", "p.value")


G3A <- ggplot(G3_prejudice_1,
       aes(x = estimate, y = level, yend = level, color = feature, fill = respondent_type, shape = respondent_type)) +
     my.theme(base_size = 8,
              borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "Coefficient", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.2, 0.5)) +
     scale_x_continuous(breaks = seq(-0.2, 0.5, by = 0.1),
                        labels = c("-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_segment(data = filter(G3_prejudice_1, !is.na(std.error) & respondent_type == "Citizen sample"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0.2)) +
     geom_point(data = filter(G3_prejudice_1, !is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2.75, position = position_nudge(x = 0, y = 0.2), stroke = 1) +
     geom_segment(data = filter(G3_prejudice_1, !is.na(std.error) & respondent_type == "Politician sample"),
                  aes(x = lower, xend = upper), 
                  size = 0.5, position = position_nudge(x = 0, y = -0.2)) +
     geom_point(data = filter(G3_prejudice_1, !is.na(std.error) & respondent_type == "Politician sample"),
                size = 2.75, position = position_nudge(x = 0, y = -0.2), stroke = 1) +
     geom_point(data = filter(G3_prejudice_1, is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", fill = "black") +
     geom_label(data = filter(G3_prejudice_1, level == "Woman politician\nx gendered text"),
               aes(x = lower, y = level, label = respondent_type),
               hjust = 1.05, vjust = 0.3, fontface = "bold",
               label.padding = unit(0.1, "lines"), label.size = NA,
               label.r = unit(0.15, "lines"),
               position = position_dodge(width = -0.8), size = 2.5) +
     scale_color_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     scale_fill_manual(values = c("white", "white")) +
     scale_shape_manual(values = c(16, 21)) +
     theme(legend.position = "none",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 8, face = "bold"))

G3B <- ggplot(G3_prejudice_2,
       aes(x = estimate, y = level, yend = level, color = feature, fill = respondent_type, shape = respondent_type)) +
     my.theme(base_size = 8,
              borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "Coefficient", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.2, 0.5)) +
     scale_x_continuous(breaks = seq(-0.2, 0.5, by = 0.1),
                        labels = c("-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_segment(data = filter(G3_prejudice_2, !is.na(std.error) & respondent_type == "Citizen sample"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0.2)) +
     geom_point(data = filter(G3_prejudice_2, !is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2.75, position = position_nudge(x = 0, y = 0.2), stroke = 1) +
     geom_segment(data = filter(G3_prejudice_2, !is.na(std.error) & respondent_type == "Politician sample"),
                  aes(x = lower, xend = upper), 
                  size = 0.5, position = position_nudge(x = 0, y = -0.2)) +
     geom_point(data = filter(G3_prejudice_2, !is.na(std.error) & respondent_type == "Politician sample"),
                size = 2.75, position = position_nudge(x = 0, y = -0.2), stroke = 1) +
     geom_point(data = filter(G3_prejudice_2, is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", fill = "black") +
     scale_color_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     scale_fill_manual(values = c("white", "white")) +
     scale_shape_manual(values = c(16, 21)) +
     theme(legend.position = "none",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 8, face = "bold"))

cat(paste0("\n\nCitizen sample observations~$=$~",
           scales::comma(mean(unique(G3_prejudice_1$n[G3_prejudice_1$respondent_type == "Citizen sample"]))),
           " (",
           scales::comma(mean(unique(G3_prejudice_1$n_clusters[G3_prejudice_1$respondent_type == "Citizen sample"]))),
           " respondents); politician sample observations~$=$~",
           scales::comma(mean(unique(G3_prejudice_1$n[G3_prejudice_1$respondent_type == "Politician sample"]))),
           " (",
           scales::comma(mean(unique(G3_prejudice_1$n_clusters[G3_prejudice_1$respondent_type == "Politician sample"]))),
           " respondents).\n\n"))

# FIGURE 7
pdf("Figures/Figure_7-RAW.pdf", 7.5, 3.5)
ggdraw() +
  draw_plot(G3A, x = -0.02, y = 0.01, width = 0.51, height = 0.94) +
  draw_plot(G3B, x = 0.48, y = 0.01, width = 0.51, height = 0.94) +
  draw_plot_label(label = c("A.",
                            "B."),
                  x = c(0.01, 0.51), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Woman politician x gendered text",
                            "Woman politician x man user"),
                  x = c(0.035, 0.535), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1, fontface = "plain")
dev.off()




G3C <- ggplot(G3_discourage_pol_1,
       aes(x = estimate, y = level, yend = level, color = feature, fill = respondent_type, shape = respondent_type)) +
     my.theme(base_size = 8,
              borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "Coefficient", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.2, 0.5)) +
     scale_x_continuous(breaks = seq(-0.2, 0.5, by = 0.1),
                        labels = c("-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_segment(data = filter(G3_discourage_pol_1, !is.na(std.error) & respondent_type == "Citizen sample"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0.2)) +
     geom_point(data = filter(G3_discourage_pol_1, !is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2.75, position = position_nudge(x = 0, y = 0.2), stroke = 1) +
     geom_segment(data = filter(G3_discourage_pol_1, !is.na(std.error) & respondent_type == "Politician sample"),
                  aes(x = lower, xend = upper), 
                  size = 0.5, position = position_nudge(x = 0, y = -0.2)) +
     geom_point(data = filter(G3_discourage_pol_1, !is.na(std.error) & respondent_type == "Politician sample"),
                size = 2.75, position = position_nudge(x = 0, y = -0.2), stroke = 1) +
     geom_point(data = filter(G3_discourage_pol_1, is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", fill = "black") +
     scale_color_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     scale_fill_manual(values = c("white", "white")) +
     scale_shape_manual(values = c(16, 21)) +
     theme(legend.position = "none",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 8, face = "bold"))

G3D <- ggplot(G3_discourage_pol_2,
       aes(x = estimate, y = level, yend = level, color = feature, fill = respondent_type, shape = respondent_type)) +
     my.theme(base_size = 8,
              borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
              grid.y_colour = "grey65", grid.y_linetype = 3,
              grid.x_colour = "grey85", grid.x_linetype = 1) +
     labs(x = "Coefficient", y = "") +
     ggforce::facet_col( ~ feature, scales = "free_y", space = "free") +
     coord_cartesian(xlim = c(-0.2, 0.5)) +
     scale_x_continuous(breaks = seq(-0.2, 0.5, by = 0.1),
                        labels = c("-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5")) +
     geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
     geom_point(size = 0, color = "transparent") +
     geom_segment(data = filter(G3_discourage_pol_2, !is.na(std.error) & respondent_type == "Citizen sample"),
                  aes(x = lower, xend = upper),
                  size = 0.5, position = position_nudge(x = 0, y = 0.2)) +
     geom_point(data = filter(G3_discourage_pol_2, !is.na(std.error) & respondent_type == "Citizen sample"),
                size = 2.75, position = position_nudge(x = 0, y = 0.2), stroke = 1) +
     geom_segment(data = filter(G3_discourage_pol_2, !is.na(std.error) & respondent_type == "Politician sample"),
                  aes(x = lower, xend = upper), 
                  size = 0.5, position = position_nudge(x = 0, y = -0.2)) +
     geom_point(data = filter(G3_discourage_pol_2, !is.na(std.error) & respondent_type == "Politician sample"),
                size = 2.75, position = position_nudge(x = 0, y = -0.2), stroke = 1) +
     geom_point(data = filter(G3_discourage_pol_2, is.na(std.error) & respondent_type == "Politician sample"),
                size = 2, position = position_nudge(x = 0, y = 0),
                color = "black", fill = "black") +
     geom_label(data = filter(G3_discourage_pol_2, level == "Woman politician\nx male user"),
               aes(x = upper, y = level, label = respondent_type),
               hjust = -0.05, vjust = 0.3, fontface = "bold",
               label.padding = unit(0.1, "lines"), label.size = NA,
               label.r = unit(0.15, "lines"),
               position = position_dodge(width = -0.8), size = 2.5) +
     scale_color_manual(values = c("#5200FF", "#00bd8e", "#FF3E00", "#A1B811")) +
     scale_fill_manual(values = c("white", "white")) +
     scale_shape_manual(values = c(16, 21)) +
     theme(legend.position = "none",
           strip.text.x = element_text(angle = 0, hjust = 0, size = 8, face = "bold"))

# FIGURE F7
pdf("Figures/Mechanisms_Interaction_Discourage_Pol-RAW.pdf", 7.5, 3.5)
ggdraw() +
  draw_plot(G3C, x = -0.02, y = 0.01, width = 0.51, height = 0.94) +
  draw_plot(G3D, x = 0.48, y = 0.01, width = 0.51, height = 0.94) +
  draw_plot_label(label = c("A.",
                            "B."),
                  x = c(0.01, 0.51), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Woman politician x gendered text",
                            "Woman politician x man user"),
                  x = c(0.035, 0.535), y = c(0.992, 0.992),
                  size = 8, hjust = 0, vjust = 1, fontface = "plain")
dev.off()




# Complete regression results for Figure 7
ms_object_figure_7A_Pol <- get_regression_table(filter(model_prejudice_interactions_1, respondent_type == "Politician sample"))
ms_object_figure_7A_Cit <- get_regression_table(filter(model_prejudice_interactions_1, respondent_type == "Citizen sample"))

ms_object_figure_7B_Pol <- get_regression_table(filter(model_prejudice_interactions_2, respondent_type == "Politician sample"))
ms_object_figure_7B_Cit <- get_regression_table(filter(model_prejudice_interactions_2, respondent_type == "Citizen sample"))

# TABLE N21
modelsummary(list("Politician" = ms_object_figure_7A_Pol,
                  "Citizen" = ms_object_figure_7A_Cit,
                  "Politician" = ms_object_figure_7B_Pol,
                  "Citizen" = ms_object_figure_7B_Cit),
             coef_map = c(setNames("Woman politician $\\times$ gendered text", "Woman politician\nx gendered text"),
                          setNames("Woman politician $\\times$ man user", "Woman politician\nx male user"),
                          setNames("Woman politician", "Woman politician"),
                          setNames("Gendered text", "Gendered text"),
                          setNames("Man user", "Man user"),
                          setNames("Co-partisan", "Co-partisan"),
                          setNames(paste0("Politician text ", 1:20), paste0("Politician text ", 1:20)),
                          setNames(paste0("User text ", 1:16), paste0("User text ", 1:16))),
             stars = c("*" = .05, "**" = .01, "***" = .001),
             output = "latex", escape = FALSE)


# APPENDIX G: Do the gender biased mechanisms predict perceived levels of toxcity
model_incivility <- feols(incivility ~ text_pol_group + text_user + party_pol_copartisan + woman_pol + woman_user + gendered + woman_user | country, cluster = ~ id, data = V)
model_incivility_resp_fe <- feols(incivility ~ text_pol_group + text_user + party_pol_copartisan + woman_pol + woman_user + gendered + woman_user | country + id, cluster = ~ id, data = V)

model_incivility_mechanisms <- feols(incivility ~ scale(opinion_diff) + scale(discourage_pol) + scale(prejudice) + text_pol_group + text_user + party_pol_copartisan + woman_pol + gendered + woman_user | country, cluster = ~ id, data = V)
model_incivility_mechanisms_resp_fe <- feols(incivility ~ scale(opinion_diff) + scale(discourage_pol) + scale(prejudice) + text_pol_group + text_user + party_pol_copartisan + woman_pol + gendered + woman_user | country + id, cluster = ~ id, data = V)

# TABLE G14
modelsummary(list("(1)" = model_incivility,
                  "(2)" = model_incivility_mechanisms,
                  "(3)" = model_incivility_mechanisms_resp_fe),
             title = "\\textbf{Relationship between gender-based mechanisms and respondents' rating of the toxicity of a social media interaction (single vignette design).} Standard errors, in parentheses, are clustered at the level of the respondent.\\label{table:mechanisms_toxicity}",
             coef_map = c("woman_polWoman politician" = "Woman politician",
                          "party_pol_copartisanCo-partisan" = "Co-partisan",
                          "genderedGendered text" = "Gendered text",
                          "woman_userMan user" = "Man user",
                          "scale(prejudice)" = "Motivated by prejudice",
                          "scale(discourage_pol)" = "To discourage politician from being in politics",
                          "scale(opinion_diff)" = "Opinion difference with politician"),
             output = "latex", statistic = c("std.error"),
             stars = c("*" = .05, "**" = .01, "***" = 0.001),
             gof_map = c("nobs"),
             add_rows = tibble(x1 = c("Respondent FE"),
                               x2 = c(""),
                               x3 = c(""),
                               x4 = c("\\checkmark")),
             escape = FALSE)




# Woman politician incivility outcome by task
Task_Vignette <- bind_rows(tibble(single_vignette("incivility",
                                                  data1 = filter(V, respondent_type == "Politician"),
                                                  data2 = filter(V, respondent_type == "Citizen"),
                                                  label1 = "Politician sample", label2 = "Citizen sample",
                                                  group = "",
                                                  formula = ~ text_pol + text_user +
                                                              party_pol_copartisan +
                                                              woman_pol +
                                                              woman_pol * task_number +
                                                              gendered +
                                                              woman_user + woman_pol |
                                                              country | 0 | id)))


Task_Vignette$level <- str_replace_all(Task_Vignette$level, "task_number", "")
Task_Vignette$level <- str_replace_all(Task_Vignette$level, "Woman pol\\.", "Woman politician")

Task_Vignette$respondent_type <- factor(Task_Vignette$respondent_type,
                                        levels = c("Politician sample",
                                                   "Citizen sample"))

Task_Vignette$level <- factor(Task_Vignette$level,
                              levels =  c("Co-partisan",
                                          "Man user",
                                          "Gendered text",
                                          "Woman politician x Task 2",
                                          "Woman politician"))

Task_Vignette$task_binary <- ifelse(Task_Vignette$level %in%
                                    c("Woman politician x Task 2"),
                                      "Task interaction", "Other")

G_Task_Vignette <- filter(Task_Vignette, feature %in% c("Co-partisan politician",
                                                        "Gender of politician",
                                                        "Gendered text",
                                                        "Gender of user",
                                                        "Interaction") &
                                         !is.na(std.error)) %>%
                   ggplot(aes(x = estimate, y = level, yend = level,
                              color = feature, fill = feature)) +
                        my.theme(base_size = 8,
                                 borderless = 2, remove.ticks.y = TRUE, remove.ticks.x = TRUE,
                                 grid.y_colour = "grey65", grid.y_linetype = 3,
                                 grid.x_colour = "grey85", grid.x_linetype = 1) +
                        labs(x = "AMCE (in percentage points)", y = "") +
                        facet_wrap( ~ respondent_type, scales = "fixed") +
                        coord_cartesian(xlim = c(-.5, .5)) +
                        scale_x_continuous(breaks = seq(-1, 1, by = 0.2),
                                           labels = c("-1", "-.8", "-.6", "-.4", "-.2", "0", ".2", ".4", ".6", ".8", ".10")) +
                        geom_vline(xintercept = 0, linetype = 1, size = 0.35, color = "grey50") +
                        geom_point(size = 0, color = "transparent") +
                        geom_segment(aes(x = lower, xend = upper), size = 0.5) +
                        geom_point(size = 2.25, stroke = 1, shape = 21) +
                        scale_color_manual(values = c("#5200FF", "#5200FF", "grey40", "grey40", "grey40")) +
                        scale_fill_manual(values = c("white", "#5200FF", "grey40", "grey40", "grey40")) +
                        theme(legend.position = "none",
                              strip.text.x = element_text(color = "transparent", angle = 0, hjust = 0.5, size = 8))

# FIGURE J12
pdf("Figures/Task_Number_Vignette-RAW.pdf", 7.5, 1.75)
ggdraw() +
  draw_plot(G_Task_Vignette, x = 0, y = 0, width = 1, height = 0.98) +
  draw_plot_label(label = c("A.", "B."),
                x = c(0.21, 0.61), y = c(0.992, 0.992),
                size = 8, hjust = 0, vjust = 1) +
  draw_plot_label(label = c("Politician sample",
                            "Citizen sample"),
                  x = c(0.23, 0.63), y = c(0.992, 0.992),
                  fontface = "bold",
                  size = 8, hjust = 0, vjust = 1)
dev.off()


